package com.xzx.flink.streamapi.processfunction;

import com.xzx.flink.bean.ClickEvent;
import com.xzx.flink.streamapi.source.ClickSource;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessAllWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.SlidingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @version 1.0
 * @auther xinzhixuan
 * @date 2022/5/4 15:28
 */
public class TopN_01_ProcessAllWindowFunction {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        env.addSource(new ClickSource())
                .assignTimestampsAndWatermarks(WatermarkStrategy.<ClickEvent>forMonotonousTimestamps()
                        .withTimestampAssigner(new SerializableTimestampAssigner<ClickEvent>() {
                            @Override
                            public long extractTimestamp(ClickEvent element, long recordTimestamp) {
                                return element.getTimestamp();
                            }
                        }))
                .windowAll(SlidingEventTimeWindows.of(Time.minutes(10), Time.seconds(5)))
                .process(new ProcessAllWindowFunction<ClickEvent, String, TimeWindow>() {
                    @Override
                    public void process(ProcessAllWindowFunction<ClickEvent, String, TimeWindow>.Context context, Iterable<ClickEvent> elements, Collector<String> out) throws Exception {
                        Map<String, Integer> urlMap = new HashMap<>();
                        for (ClickEvent element : elements) {
                            urlMap.merge(element.getUrl(), 1, Integer::sum);
                        }

                        List<Tuple2<String, Integer>> list = urlMap.entrySet().stream()
                                .map(d -> Tuple2.of(d.getKey(), d.getValue()))
                                .sorted((o1, o2) -> -o1.f1.compareTo(o2.f1))
                                .collect(Collectors.toList());


                        // 构建输出
                        for (int i = 0; i < 2 && i < list.size(); i++) {
                            Tuple2<String, Integer> tuple2 = list.get(i);
                            String info = "浏览量No." + (i + 1) + " url:" + tuple2.f0 + ";浏览量:" + tuple2.f1 + ";窗口结束时间：" + new Timestamp(context.window().getEnd());
                            out.collect(info);
                        }
                        out.collect("============分割线====================");
                    }
                })
                .print();

        env.execute(TopN_01_ProcessAllWindowFunction.class.getSimpleName());
    }
}
